﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System.Linq;
using System.Drawing;
using System.Windows.Forms;

namespace TeachMe.Application.Configuration
{
    public class WindowProportions
    {
        public string Name { get; set; }

        public Size Size { get; set; }

        public Point Location { get; set; }

        public bool Maximized { get; set; }

        public static void Lock(Form form, MachineSettings machineSettings)
        {
            WindowProportions firstOrDefault = machineSettings.WindowsProportions.FirstOrDefault(x => x.Name == form.Name);
            bool center = true;

            if (firstOrDefault != null)
            {
                WindowLocations wl = new WindowLocations();
                wl.Populate(firstOrDefault.Location.X, firstOrDefault.Location.Y, firstOrDefault.Size);

                if (firstOrDefault.Maximized)
                {
                    form.WindowState = FormWindowState.Maximized;
                    
                    center = false;
                }
                else if (wl.IsFullyVisible())
                {
                    form.Location = firstOrDefault.Location;
                    form.Size = firstOrDefault.Size;

                    center = false;
                }
                else
                {
                    Logging.Warn(typeof(WindowProportions), "Invalid form '{0}' placement.", form.Name);
                }
            }

            if (center)
            {
                Size size = SystemInformation.PrimaryMonitorSize;
                form.Location = new Point((size.Width / 2) - (form.Width / 2), (size.Height / 2) - (form.Height / 2));
            }

            form.Closing += delegate
                                {
                                    WindowProportions windowProportions = null;

                                    foreach (WindowProportions item in machineSettings.WindowsProportions)
                                    {
                                        if (form.Name == item.Name)
                                        {
                                            windowProportions = item;
                                        }
                                    }

                                    using (machineSettings.Edit())
                                    {
                                        if (windowProportions == null)
                                        {
                                            windowProportions = new WindowProportions();
                                            windowProportions.Name = form.Name;

                                            machineSettings.WindowsProportions.Add(windowProportions);
                                        }

                                        if (form.WindowState == FormWindowState.Maximized)
                                        {
                                            windowProportions.Maximized = true;
                                        }
                                        else
                                        {
                                            windowProportions.Maximized = false;
                                            windowProportions.Location = form.Location;
                                            windowProportions.Size = form.Size;
                                        }
                                    }
                                };
        }
    }
}
